//
//  HXMarketViewController.m
//  爱鲜蜂
//
//  Created by 建国 on 16/3/27.
//  Copyright © 2016年 建国. All rights reserved.
//

#import "HXMarketViewController.h"
#import "HXCategoryViewController.h"
#import "HXProductViewController.h"
#import "HXQRCodeVC.h"
#import "HXButton.h"
#import "HXSerachVC.h"
#import "HXTopTitleView.h"
#import "HXCategories.h"
#import <MJExtension.h>

@interface HXMarketViewController ()

@property (nonatomic,strong) NSArray *categories;

@end

@implementation HXMarketViewController

//懒加载目录的数据
-(NSArray *)categories{

    if (_categories == nil) {
        
        NSMutableArray *categories = [NSMutableArray array];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"supermarket" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *test = json[@"data"][@"categories"];
        //字典数组转模型
        for (NSDictionary *dict in test) {
            HXCategories *cate = [HXCategories mj_objectWithKeyValues:dict];
            //进行赋值
            [categories addObject:cate];
        }
        
        _categories = categories;
        
    }
    

    return _categories;
}

//懒加载 商品的数据

-(void)viewDidLoad{
  
    [self setupTwoTableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //设置导航栏
    [self setupNavigationItem];

//    NSLog(@"%@",json);
//    [json writeToFile:@"/Users/hongdongheng/Desktop/video.plist" atomically:YES];
    
}
//创建两个tableView
-(void)setupTwoTableView{
    
    HXCategoryViewController *catrgoryVC = [[HXCategoryViewController alloc]init];
    catrgoryVC.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH /4, SCREEN_HEIGHT);
    //赋值
    catrgoryVC.categories = self.categories;
    
    if(![self.view.subviews containsObject:catrgoryVC.tableView]){
        [self.view addSubview:catrgoryVC.tableView];
        [self addChildViewController:catrgoryVC];
    }
    
    HXProductViewController *productVC = [[HXProductViewController alloc]init];
    productVC.tableView.frame = CGRectMake(SCREEN_WIDTH * 0.25 , 0, SCREEN_WIDTH * 0.75, SCREEN_HEIGHT);
    productVC.tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    if(![self.view.subviews containsObject:productVC.tableView]){
        [self.view addSubview:productVC.tableView];
        [self addChildViewController:productVC];
    }
}

#pragma mark - 设置导航条信息 -
-(void)setupNavigationItem{
    
    HXButton *leftButton = [HXButton buttonWithTitle:@"扫一扫" NormalImage:@"icon_black_scancode" HighlightedImage:@"scanicon" andFontSize:10];
    
    [leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    HXButton *rightButton = [HXButton buttonWithTitle:@"搜索" NormalImage:@"icon_search" HighlightedImage:nil andFontSize:10];
    
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarItem ;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"v2_my_avatar_bg"] forBarMetrics:(UIBarMetricsDefault)];
    
    HXTopTitleView *titleView = [HXTopTitleView topTitleView];
    titleView.frame = CGRectMake(0, 0, 120, 30);
    
    self.navigationItem.titleView = titleView;
    
   

}

#pragma mark - 导航按钮点击
-(void)leftButtonClick{
    HXQRCodeVC *qrCode = [[HXQRCodeVC alloc]init];
    [self.navigationController pushViewController:qrCode animated:YES];
}

-(void)rightButtonClick{
    
    HXSerachVC *search = [[HXSerachVC alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    
}


#pragma mark - 导航按钮点击



#pragma mark - 待解决问题 -
 



@end
